ige.controller('DepartmentController', ['$scope', 'DepartmentService', '$mdToast', '$state', 'EventBus', '$timeout', function ($scope, DepartmentService, $mdToast, $state, EventBus, $timeout) {
    var pageSkip = 0;
    var currentDeleteId = 0;
    var deleteArray = [];
    $scope.filter = null;
    var timer;
    $scope.noNewData = false;
    EventBus.off("department.keywordSearch");
    EventBus.on("department.keywordSearch", function (event) {
        $timeout.cancel(timer);
        timer = $timeout(
            function () {
                pageSkip = 0;
                $scope.Departments = [];
                $scope.filter = event.data;
                $scope.getDepartments();
            }, 300
        );
    });

    $scope.$on(
        "$destroy",
        function (event) {
            $timeout.cancel(timer);
        }
    );

    $scope.Departments = [];
    $scope.loadMore = false;
    $scope.loaddingListData = true;
    $scope.getDepartments = function () {
        $scope.loaddingListData = true;
        DepartmentService.getDepartments(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
            $scope.Departments = $scope.Departments.concat(data);
            if (data.length == 0) {
                $scope.noNewData = true;
            } else {
                $scope.noNewData = false;
            }
        }).then(function () {
            pageSkip += ige.systemSetting.defaultPageSize;
        }).then(function () {
            $scope.loaddingListData = false;
        });
    };

    $scope.loadMoreData = function () {
        $scope.loadMore = true;
        DepartmentService.getDepartments(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
            $scope.Departments = $scope.Departments.concat(data);
            if (data.length == 0) {
                $scope.noNewData = true;
            } else {
                $scope.noNewData = false;
            }
        }).then(function () {
            pageSkip += ige.systemSetting.defaultPageSize;
        }).then(function () {
            $scope.loadMore = false;
        });
    };

    $scope.edit = function (id, name, desc) {
        $state.go('MainNav.EditDepartment', {id: id, name: name, desc: desc});
    };
    $scope.getDepartments();
    $scope.itemHide = function (id) {
        return deleteArray.indexOf(id) > -1;
    };

    $scope.loadding = function (id) {
        return id == currentDeleteId;
    };
    var deleteSuccess = false;
    $scope.delete = function (id) {
        currentDeleteId = id;
        DepartmentService.deleteDepartment(id, function (data) {
            deleteSuccess = true;
        }).then(function () {
            if (deleteSuccess) {
                currentDeleteId = 0;
                deleteArray.push(id);
            }
            deleteSuccess = false;
        });
    }
}]);

ige.controller('AddDepartmentController', ['$scope', 'DepartmentService', '$mdToast', '$state', function ($scope, DepartmentService, $mdToast, $state) {
    $scope.loadding = false;
    var addSuccess = false;
    $scope.Department = {
        Name: undefined,
        Description: undefined
    };

    $scope.addDepartment = function () {
        $scope.loadding = true;
        DepartmentService.addDepartment($scope.Department.Name, $scope.Department.Description, function (data) {
            addSuccess = true;
        }).then(function () {
            if (addSuccess) {
                $state.go('MainNav.Department');
            } else {
            }
            $scope.loadding = false;
            addSuccess = false;

        });
    }

}]);

ige.controller('EditDepartmentController', ['$scope', '$stateParams', 'DepartmentService', '$mdToast', '$state', function ($scope, $stateParams, DepartmentService, $mdToast, $state) {
    var id = $stateParams.id;
    var name = $stateParams.name;
    var desc = $stateParams.desc;
    var editSuccess = false;
    $scope.Department = {
        Name: name,
        Description: desc
    };

    $scope.editDepartment = function () {
        $scope.loadding = true;
        DepartmentService.editDepartment(id, $scope.Department.Name, $scope.Department.Description, function (data) {
            editSuccess = true;
        }).then(function () {
            if (editSuccess) {
                $state.go('MainNav.Department');
            }

            $scope.loadding = false;
            editSuccess = false
        });
    }
}]);